package com.jeecg.p3.system.poster.service;

import com.jeecg.p3.base.util.DateUtil;
import com.jeecg.p3.system.poster.dao.ExamResultDao;
import com.jeecg.p3.system.poster.dao.ExamResultDao;
import com.jeecg.p3.system.poster.dao.ExamResultDetailDao;
import com.jeecg.p3.system.poster.entity.ExamResult;
import com.jeecg.p3.system.poster.entity.ExamResultDetail;
import com.jeecg.p3.system.util.UuidUtils;
import org.jeecgframework.p3.core.utils.common.PageList;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.PageQueryWrapper;
import org.jeecgframework.p3.core.utils.common.Pagenation;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 *
 * @ClassName: ExamResultService
 * @Description: TODO 考试结果数据处理层
 * @author: zhangxiao
 * @date: 2021年1月10日 下午7:29:38
 */
@Service
public class ExamResultService {
    @Resource
    private ExamResultDao dao;
    @Resource
    private ExamResultDetailDao detailDao;

    /**
     * 分页查询考试结果
     * @param pageQuery
     * @return
     */
    public PageList<ExamResult> queryPageList(PageQuery<ExamResult> pageQuery) {
        PageList<ExamResult> result = new PageList<>();
        Integer itemCount = dao.count(pageQuery);
        PageQueryWrapper<ExamResult> wrapper = new PageQueryWrapper<>(pageQuery.getPageNo(), pageQuery.getPageSize(),itemCount, pageQuery.getQuery());
        List<ExamResult> list = dao.queryPageList(wrapper);
        Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount, pageQuery.getPageSize());
        result.setPagenation(pagenation);
        result.setValues(list);
        return result;
    }

    /**
     * 根据传参获取考试结果数量
     * @param result
     * @return
     */
    public Integer count(ExamResult result) {
        PageQuery<ExamResult> pageQuery = new PageQuery<>();
        pageQuery.setQuery(result);
        return dao.count(pageQuery);
    }

    /**
     * 新增考试结果信息
     * @param result
     */
    @Transactional(rollbackFor = Exception.class)
    public void doAdd(ExamResult result) {
        Date date = new Date();
        String resultId = UuidUtils.getId();
        List<ExamResultDetail> details = result.getDetailList();
        for(ExamResultDetail detail: details) {
            detail.setExamResultId(resultId);
            insertDetail(detail, date);
        }
        result.setStatus("1");
        result.setCreateDate(DateUtil.dateToString(date));
        result.setCreateTime(DateUtil.dateToString(date, "HH:mm:ss"));
        result.setId(resultId);
        dao.insert(result);
    }

    /**
     * 添加考试结果详情
     * @param detail
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertDetail(ExamResultDetail detail, Date date) {
        detail.setStatus("1");
        detail.setCreateDate(DateUtil.dateToString(date));
        detail.setCreateTime(DateUtil.dateToString(date, "HH:mm:ss"));
        detail.setId(UuidUtils.getId());
        detailDao.insert(detail);
    }

    /**
     * 根据id查询考试结果信息
     * @param id
     * @return
     */
    public ExamResult findById(String id) {
        return dao.get(id);
    }

    /**
     * 根据传参获取考试结果列表信息
     * @param result
     * @return
     */
    public List<ExamResult> findByParam(ExamResult result) {
        if(result == null) {
            result = new ExamResult();
        }
        return dao.findByParam(result);
    }

    /**
     * 查询考试结果详情
     * @param detail
     * @return
     */
    public List<ExamResultDetail> findByParam(ExamResultDetail detail) {
        if(detail == null) {
            detail = new ExamResultDetail();
        }
        return detailDao.findByParam(detail);
    }

    /**
     * 编辑考试结果信息
     * @param result
     */
    public void doEdit(ExamResult result) {
        dao.update(result);
    }

    /**
     * 删除考试结果信息
     * @param result
     */
    public void doDelete(ExamResult result) {
        result.setStatus("0");
        dao.update(result);
    }
}
